// 对象数组去重
function uniqueMultiPropertyArray(arr) {
	const uniqueArr = arr.filter((item, index, self) => {
		return index === self.findIndex(obj => obj.Id === item.Id && obj.cCusName === item.cCusName);
	});

	return uniqueArr
}

function getFileNameFromUrl(url) {
	return url.split('/').pop();
}

function momenyFormat(num) {
	num = Number(num)
	if (isNaN(num)) return '0.00'
	return num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')
}
// 金额大写处理函数
function transformToUppercase(money) {
	// 初始化的时候会出现undefined
	if (!money) return
	//  将数字金额转换为大写金额
	const cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖') // 汉字的数字
	const cnIntRadice = new Array('', '拾', '佰', '仟') // 基本单位
	const cnIntUnits = new Array('', '万', '亿', '兆') // 对应整数部分扩展单位
	const cnDecUnits = new Array('角', '分', '毫', '厘') // 对应小数部分单位
	const cnInteger = '整' // 整数金额时后面跟的字符
	const cnIntLast = '元' // 整数完以后的单位
	const maxNum = 999999999999999.9999 // 最大处理的数字
	let integerNum // 金额整数部分
	let decimalNum // 金额小数部分
	let chineseStr = '' // 输出的中文金额字符串
	if (money == '') {
		return ''
	}
	money = parseFloat(money)
	if (money >= maxNum) {
		return '超出最大处理数字'
	}
	if (money == 0) {
		chineseStr = cnNums[0] + cnIntLast + cnInteger
		return chineseStr
	}
	// 四舍五入保留两位小数,转换为字符串
	money = Math.round(money * 100).toString()
	integerNum = money.substr(0, money.length - 2)
	decimalNum = money.substr(money.length - 2)
	// 获取整型部分转换
	if (parseInt(integerNum, 10) > 0) {
		let zeroCount = 0
		const IntLen = integerNum.length
		for (let i = 0; i < IntLen; i++) {
			const n = integerNum.substr(i, 1)
			const p = IntLen - i - 1
			const q = p / 4
			const m = p % 4
			if (n == '0') {
				zeroCount++
			} else {
				if (zeroCount > 0) {
					chineseStr += cnNums[0]
				}
				// 归零
				zeroCount = 0
				chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
			}
			if (m == 0 && zeroCount < 4) {
				chineseStr += cnIntUnits[q]
			}
		}
		chineseStr += cnIntLast
	}
	// 小数部分
	if (decimalNum != '') {
		const decLen = decimalNum.length
		for (let i = 0; i < decLen; i++) {
			const n = decimalNum.substr(i, 1)
			if (n != '0') {
				chineseStr += cnNums[Number(n)] + cnDecUnits[i]
			}
		}
	}
	if (chineseStr == '') {
		chineseStr += cnNums[0] + cnIntLast + cnInteger
	} else if (decimalNum == '' || /^0*$/.test(decimalNum)) {
		chineseStr += cnInteger
	}
	return chineseStr
}
/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
function showConfirm(obj) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: obj.content,
			cancelText: obj.cancelText ? obj.cancelText : '取消', // 取消按钮的文字  
			confirmText: obj.confirmText ? obj.confirmText : '确定', // 确认按钮文字  
			confirmColor: obj.confirmColor ? obj.confirmColor : '', //删除字体的颜色
			cancelColor: obj.cancelColor ? obj.cancelColor : '', //取消字体的颜色
			success: function(res) {
				resolve(res)
			}
		})
	})
}
// 格式化成年月日
// 使用方式
// var year = getDateTime('year');
// console.log(year);  // 2021
// var month = getDateTime('month');
// console.log(month); // 2021-12
// var date = getDateTime('');
// console.log(date);  // 2021-12-03 09:00:00
// 日期格式化年月日函数
function formatDate(type, format) {
	var date = new Date();
	var hengGang = "-";
	var maoHao = ":";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var curDate = date.getDate();
	var curHours = date.getHours();
	var curMinutes = date.getMinutes();
	var curSeconds = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (curDate >= 0 && curDate <= 9) {
		curDate = "0" + curDate;
	}
	if (curHours >= 0 && curHours <= 9) {
		curHours = "0" + curHours;
	}
	if (curMinutes >= 0 && curMinutes <= 9) {
		curMinutes = "0" + curMinutes;
	}
	if (curSeconds >= 0 && curSeconds <= 9) {
		curSeconds = "0" + curSeconds;
	}
	var currentdate = "";
	if (type == "format") {
		// 传递需要的日期 格式化成年月日 只要年月日  format是传递的日期参数
		currentdate = format.slice(0, 16);
		return currentdate;
	} else if (type == "year") {
		// 获取年份
		currentdate = year;
		return currentdate;
	} else if (type == "month") {
		// 获取年份月份
		currentdate = year + hengGang + month;
		return currentdate;
	} else if (type == "day") {
		// 获取年月日
		currentdate = year + hengGang + month + hengGang + curDate;
		return currentdate;
	} else if (type == "minutes") {
		// 获取年月日 时分
		currentdate =
			year +
			hengGang +
			month +
			hengGang +
			curDate +
			" " +
			curHours +
			maoHao +
			curMinutes;
	} else {
		// 获取年月日 + 当前时间
		currentdate =
			year +
			hengGang +
			month +
			hengGang +
			curDate +
			" " +
			curHours +
			maoHao +
			curMinutes +
			maoHao +
			curSeconds;
		return currentdate;
	}
}

var common = {}
//安卓本地文件选择器 
common.androidChooseFile = (callback, acceptType) => {
	var CODE_REQUEST = 1000;
	var main = plus.android.runtimeMainActivity();
	if (plus.os.name == 'Android') {
		var Intent = plus.android.importClass('android.content.Intent');
		var intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		if (acceptType) {
			intent.setType(acceptType);
		} else {
			intent.setType("*/*");
		}
		main.onActivityResult = (requestCode, resultCode, data) => {
			if (requestCode == CODE_REQUEST) {
				const uri = data.getData();
				plus.android.importClass(uri);
				const Build = plus.android.importClass('android.os.Build');
				const isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
				const DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');
				if (isKitKat && DocumentsContract.isDocumentUri(main, uri)) {
					if ("com.android.externalstorage.documents" == uri.getAuthority()) {
						console.log("6666");
						var docId = DocumentsContract.getDocumentId(uri);
						var split = docId.split(":");
						var type = split[0];

						if ("primary" == type) {
							var Environment = plus.android.importClass('android.os.Environment');
							callback(Environment.getExternalStorageDirectory() + "/" + split[1]);
						} else {
							var System = plus.android.importClass('java.lang.System');
							var sdPath = System.getenv("SECONDARY_STORAGE");
							if (sdPath) {
								callback(sdPath + "/" + split[1]);
							}
						}
					} else if ("com.android.providers.downloads.documents" == uri.getAuthority()) {
						var id = DocumentsContract.getDocumentId(uri);
						var ContentUris = plus.android.importClass('android.content.ContentUris');
						var contentUri = ContentUris.withAppendedId(
							//    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
							Uri.parse("content://downloads/public_downloads"), id);
						callback(getDataColumn(main, contentUri, null, null));
					} else if ("com.android.providers.media.documents" == uri.getAuthority()) {
						var docId = DocumentsContract.getDocumentId(uri);
						var split = docId.split(":");
						console.log(split);
						var type = split[0];
						console.log(type);
						var MediaStore = plus.android.importClass('android.provider.MediaStore');
						if ("image" == type) {
							contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
						} else if ("video" == type) {
							contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
						} else if ("audio" == type) {
							contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
						} else {
							contentUri = MediaStore.Files.getContentUri("external");
						}

						console.log(contentUri);
						var selection = "_id=?";
						var selectionArgs = new Array();
						selectionArgs[0] = split[1];

						callback(getDataColumn(main, contentUri, selection, selectionArgs));
					}
				} else if ("content" == uri.getScheme()) {
					callback(getDataColumn(main, uri, null, null));
				} else if ("file" == uri.getScheme()) {
					callback(uri.getPath());
				}
			}
		}
		main.startActivityForResult(intent, CODE_REQUEST);
	}

	function getDataColumn(main, uri, selection, selectionArgs) {
		plus.android.importClass(main.getContentResolver());
		let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,
			null);
		plus.android.importClass(cursor);
		if (cursor != null && cursor.moveToFirst()) {
			var column_index = cursor.getColumnIndexOrThrow('_data');
			var result = cursor.getString(column_index)
			cursor.close();
			return result;
		}
		return null;
	}
}
export default {
	momenyFormat,
	showConfirm,
	formatDate,
	transformToUppercase,
	common,
	getFileNameFromUrl,
	uniqueMultiPropertyArray
}